import unittest
import paramunittest
import readConfig as readConfig
from common import configHttp
from common import businessCommon
from common import common
from common.Log import MyLog

localReadConfig = readConfig.ReadConfig()
localConfigHttp = configHttp.ConfigHttp()
localUpdatePassword_xls = common.get_xls("userCase.xlsx", "updatePassword")


@paramunittest.parametrized(*localUpdatePassword_xls)
class UpdatePassword(unittest.TestCase):

    def setParameters(self, case_name, method, mobile, password, result, code, msg):
        """
        set parameters
        :param case_name:
        :param method:
        :param mobile:
        :param password:
        :param result:
        :param code:
        :param msg:
        :return:
        """
        self.case_name = str(case_name)
        self.method = str(method)
        self.mobile = str(mobile)
        self.pwd = str(password)
        self.result = str(result)
        self.code = str(code)
        self.msg = str(msg)
        self.response = None
        self.info = None
        self.login_token = None

    def description(self):
        """

        :return:
        """
        self.case_name

    def setUp(self):
        """
        :return:
        """
        self.log = MyLog.get_log()
        self.logger = self.log.get_logger()
        # login
        #self.login_token = businessCommon.login()

        return

    def testUpdatePassword(self):
        """
        test body
        :return:
        """

        # set url
        self.url = common.get_url_from_xml('updatePassword')
        localConfigHttp.set_url(self.url)

        # set param
        data = {"password": self.pwd, "mobile": self.mobile}
        print('===========data',data)
        localConfigHttp.set_data(data)

        # test interface
        self.response = localConfigHttp.post()
        print("================self.return_json",self.response)
        # check result
        self.checkResult()

    def tearDown(self):
        """

        :return:
        """
        # logout
        businessCommon.logout(self.login_token)
        self.log.build_case_line(self.case_name, self.info['code'], self.info['desc'])

    def checkResult(self):
        """
        check test result
        :return:
        """
        self.info = self.response.json()

        common.show_return_msg(self.response)

        if self.result == '0':
            self.assertEqual(self.info['code'], int(self.code))
            self.assertEqual(self.info['desc'], self.msg)

        if self.result == '1':
            self.assertEqual(self.info['code'], int(self.code))
            self.assertEqual(self.info['desc'], self.msg)
